from mcp.server.fastmcp import FastMCP
import sympy as sp

# 创建 MCP 服务实例
mcp = FastMCP("Symbolic Matrix Calculator")

@mcp.tool()
def matrix_add(a: str, b: str) -> str:
    """符号矩阵加法"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        mat_b = sp.Matrix(sp.sympify(b))
        return str(mat_a + mat_b)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_multiply(a: str, b: str) -> str:
    """符号矩阵乘法"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        mat_b = sp.Matrix(sp.sympify(b))
        return str(mat_a * mat_b)
    except Exception as e:
        return f"Error: {str(e)}"

@mcp.tool()
def matrix_inverse(a: str) -> str:
    """符号矩阵求逆"""
    try:
        mat_a = sp.Matrix(sp.sympify(a))
        return str(mat_a.inv())
    except Exception as e:
        return f"Error: {str(e)}"

if __name__ == "__main__":
    mcp.run(transport="stdio")